package one.stand.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import one.stand.mapper.CompanyApplyRecordModelMapper;
import one.stand.mapper.CompanyCategoryRelationModelMapper;
import one.stand.mapper.CompanyMapperExt;
import one.stand.model.*;
import one.stand.service.CompanyApplyService;
import one.stand.service.WebsiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by yandou on 2019/6/14.
 */
@Service
public class CompanyApplyServiceImpl implements CompanyApplyService {

    @Autowired
    private CompanyMapperExt companyMapperExt;
    @Autowired
    private WebsiteService websiteService;
    @Autowired
    private CompanyApplyRecordModelMapper companyApplyRecordModelMapper;
    @Autowired
    private CompanyCategoryRelationModelMapper companyCategoryRelationModelMapper;

    @Override
    public ResultModel list(CompanyApplyRecordModel request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<CompanyCategoryModel> categoryModelList = companyApplyRecordModelMapper.selectAll(request);
        return ResultModel.success(new PageInfo(categoryModelList));
    }

    @Override
    public ResultModel updateState(CompanyApplyRecordModel request) {
        CompanyApplyRecordModel m = new CompanyApplyRecordModel();
        m.setUtime(new Date());
        m.setCompanyApplyId(request.getCompanyApplyId());
        m.setAuditState(request.getAuditState());
        companyApplyRecordModelMapper.updateByPrimaryKeySelective(m);
        // 审核通过处理
        if (request.getAuditState() != null) {
            if (request.getApplyCompanyId() != null && request.getApplyCompanyId() == 0) { // 向平台申请
                Company companyModel = new Company();
                companyModel.setCompanyId(request.getCompanyId());
                // 更新对应审核标示
                if (request.getApplyType() == CompanyApplyRecordModel.apply_type_supply) {
                    companyModel.setQjSupplyState(request.getAuditState());
                } else {
                    companyModel.setQjAgentState(request.getAuditState());
                }
                companyMapperExt.updateByPrimaryKeySelective(companyModel);
            } else { // 向供应商申请代理
                if (request.getAuditState() == CompanyApplyRecordModel.audit_success) {
                    //更新被代理次数
                    companyMapperExt.updateAgentTime(request.getApplyCompanyId());
                }
            }
        }
        return ResultModel.success();
    }

    @Override
    @Transactional
    public ResultModel add(CompanyApplyRecordModel request) {
        Company companyModel = companyMapperExt.selectByPrimaryKey(request.getCId().intValue());
        if (companyModel == null || companyModel.getIsDel() != 0) {
            return ResultModel.fail("申请企业状态异常");
        }
        // 获取地址
        Website websiteModel = websiteService.getByCompanyId(companyModel.getCompanyId());
        if (websiteModel != null) {
            request.setAddress(websiteModel.getAddress());
        }
        request.setCompanyId(companyModel.getCompanyId());
        request.setCompanyName(companyModel.getCompanyName());
        request.setCompanyLogo(companyModel.getCompanyLogo());
        request.setPhone(companyModel.getPhone());
        request.setAuditState(0);
        Date date = new Date();
        request.setCreateTime(date);
        request.setUtime(date);
        request.setAgentGoodsCnt(0);
        companyApplyRecordModelMapper.insert(request);
        // 平台申请更新标示
        if (request.getApplyCompanyId() == 0) {
            Company cm = new Company();
            cm.setCompanyId(request.getCId().intValue());
            if (request.getApplyType() == CompanyApplyRecordModel.apply_type_supply) {
                cm.setQjSupplyState(0);
            } else {
                cm.setQjAgentState(0);
            }
            companyMapperExt.updateByPrimaryKeySelective(cm);
        }
        return ResultModel.success();
    }

    @Override
    public ResultModel categoryList(CompanyCategoryRelationModel request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<CompanyCategoryRelationModel> list = companyCategoryRelationModelMapper.select(request);
        return ResultModel.success(new PageInfo(list));
    }

    @Override
    public ResultModel categoryAdd(CompanyCategoryRelationModel request) {
        Date date = new Date();
        request.setCreateTime(date);
        request.setUtime(date);
        companyCategoryRelationModelMapper.insert(request);
        return ResultModel.success();
    }

    @Override
    public ResultModel categoryDel(CompanyCategoryRelationModel request) {
        companyCategoryRelationModelMapper.deleteByPrimaryKey(request.getId().intValue());
        return ResultModel.success();
    }

    @Override
    public ResultModel applyCompanyList(CompanyApplyRecordModel request) {
        request.setCompanyId(request.getCId().intValue());
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<Map> list = companyMapperExt.applyCompanyList(request);
        return ResultModel.success(new PageInfo(list));
    }
}
